I am trying to make a bot to check if an item is in stock. I based it off of this video https://www.youtube.com/watch?v=57TEZq8O360, but changed the url. Although I can console.log the content of the webpage when I try to use cheerio like jquery I get this error "TypeError: $ is not a function". I know cheerio is installed and works as I have declared it with "const $ = require('cheerio');" and have been able to use it elsewhere in my file. Is there a typo that I am missing? My broken code is below.
async function checkStock(page){
await page.reload();
let content = await page.evaluate(() => document.body.innerHTML);
console.log(content);
$("link[itemprop='availability']", content).each(function(){ //error here
let instock= $(this).attr('href').toLowerCase().includes("outofstock");
if(instock){
console.log("no");
}
});}
You need to load the HTML into cheerio, and then cheerio will give you the $ API:
const $ = require("cheerio");
// ...
async function checkStock(page){
await page.reload();
const content = await page.evaluate(() => document.body.innerHTML);
const $ = cheerio.load(content);
$("link[itemprop='availability']", content).each(function(){ //error here
let instock = $(this).attr('href').toLowerCase().includes("outofstock");
if (instock) {
console.log("no");
}
});
}